<?php
class category_listModel extends baseModel{
    protected $table = 'catrgory';

    //获取栏目基本信息
    public function info($cid,$urlname=false)
    {
        if(!$urlname){
            $where='A.cid='.$cid;
        }else{
            $where='A.urlname="'.$cid.'"';
        }
        
        $sql="
            SELECT A.*,B.model
            FROM {$this->model->pre}category A
            INNER JOIN {$this->model->pre}model B ON B.mid =A.mid
            WHERE {$where} ORDER BY A.cid desc LIMIT 1
            ";
        $info=$this->query($sql);
        return $info[0];
    }

    //栏目列表
    public function content_list($category,$where,$limit,$list_sort)
    {
        $expand_id=$category['expand'];
        if(!empty($expand_id)){
            $model_info=api('expand_model','model_info',$expand_id);
            $expand=" LEFT JOIN {$this->model->pre}expand_content_{$model_info['table']} C ON C.aid = A.aid ";
            $expand_field="C.*,";
        }
        $filter=api('filter','category_sql',$category['filter']);
        $loop="
            SELECT distinct {$expand_field}A.*,B.name as cname,B.subname as csubname,B.mid
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON A.cid = B.cid
             {$expand}
             {$filter}
             WHERE {$where} ORDER BY {$list_sort} LIMIT {$limit}
            ";
            return $this->query($loop);
    }

    //内容统计
    public function content_count($category,$where)
    {
        $filter=api('filter','category_sql',$category['filter']);
        $sql="
            SELECT distinct count(*) as num
            FROM {$this->model->pre}content A 
            INNER JOIN {$this->model->pre}category B ON A.cid = B.cid
            {$filter}
            WHERE {$where}
            ";
        $data=$this->query($sql);
        return $data[0]['num'];
    }
    


}
